package com.wow.wa.ui;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.wow.wa.ui.bean.UICategory;
import com.wow.wa.uicommon.R;
import com.wow.wa.util.CommonUtils;

public class CategoryBtn extends TextView implements OnDateSetListener {

	private static final String FROMAT = "%03d/%02d/%02d";
	private static final String TAG = "CategoryBtn";
	private UICategory category;

	private static final String EMPTY = "";
	private List<UICategory> categorys;
	private boolean isCode;// 是否給予代號
	private Context context;
	private boolean isBind = false;
	private String title = "";

	// 代碼檔對話框
	private CategoryItemDialog categoryDialog;

	// 文字對話框
	private TextDialog textDialog;
	private BaseDialog dialog = null;

	private DatePickerDialog datePicker = null;
	private TimePickerDialog timePicker = null;
	private CategoryType type;

	public CategoryBtn(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.context = context;
		this.category = new UICategory(EMPTY,
				context.getString(R.string.clickToEnter));

		this.setCategory(category);

	}

	/**
	 * 產生代碼檔對話框
	 * 
	 * @param categorys
	 * @param isCode
	 */
	public void createCategoryDialog(String titleName,
			List<UICategory> categorys, Boolean isCode, String code, String name) {
		this.categorys = categorys;
		this.dialog = new CategoryItemDialog(titleName, context, this);
		this.setCategory(name, code);
		this.isCode = isCode;
		this.title = titleName;
		this.type = CategoryType.category;

	}

	/**
	 * 產生代碼檔對話框
	 * 
	 * @param categorys
	 * @param isCode
	 */
	public void createCategoryDialog(String titleName,
			List<UICategory> categorys, Boolean isCode, String code) {
		this.categorys = categorys;
		this.dialog = new CategoryItemDialog(titleName, context, this);
		
		this.setCode(code);
		
		
	
		this.isCode = isCode;
		this.title = titleName;
		this.type = CategoryType.category;

	}

	/**
	 * 產生編輯對話框
	 * 
	 * @param isCode
	 * @param type
	 */
	public void createEditDialog(String titleName, String text, Boolean isCode,
			int type) {
		dialog = new TextDialog(titleName, context, this, type);
		TextDialog pDialog = (TextDialog) dialog;
		pDialog.setText(text);
		this.setCategory(text, text);
		this.isCode = isCode;
		this.title = titleName;
		this.type = CategoryType.editType;

	}

	public void crateDateDialog(String yyymmddd) {
		this.datePicker = this.initDatePicker(yyymmddd);
		this.setCategory(yyymmddd, yyymmddd);

		this.type = CategoryType.date;
	}

	private DatePickerDialog initDatePicker(String yyymmddd) {

		if (StringUtils.isBlank(yyymmddd)) {
			yyymmddd = CommonUtils.getDate();
		}
		final String[] str = StringUtils.split(yyymmddd, "/");

		Integer iYear = Integer.parseInt(str[0]);
		Integer iMon = Integer.parseInt(str[1]);
		Integer iday = Integer.parseInt(str[2]);

		return new DatePickerDialog(this.context, this, iYear, iMon - 1, iday);

	}

	/**
	 * 綁定彈跳對話框
	 */
	public void bindOnClickEvent() {

		isBind = true;
		refresh();
		this.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				switch (type) {
				case category:
					dialog.show();
					break;
				case editType:
					dialog.show();
					break;

				case none:
					break;
				case date:
					datePicker.show();
					break;

				case time:
					timePicker.show();
					break;
				}

			}

		});
	}

	/**
	 * 取消綁定
	 */
	public void unBindOnClickEvent() {
		isBind = false;
		refresh();
		this.setOnClickListener(null);
	}

	public void init(List<UICategory> categorys, Boolean isCode,
			CategoryType type) {
		this.categorys = categorys;
		this.isCode = isCode;
		switch (type) {
		case editType:
			break;
		case category:
			break;
		case none:
			break;

		case date:
			break;

		case time:
			break;
		}
	}

	public void setCode(String code) {
		for (UICategory category : categorys) {
			if (category.getCode().equals(code)) {
				this.setCategory(category.getName(), category.getCode());

			}
		}
	}

	public void setName(String name) {
		for (UICategory category : categorys) {
			if (category.getName().equals(name)) {
				this.setCategory(category.getName(), category.getCode());

			}
		}
	}

	// 取得值
	public String getBeanValue() {
		if (isCode) {
			return category.getCode();
		} else {

			if (StringUtils.isNotBlank(category.getCode())) {
				return category.getName();
			} else {
				return StringUtils.EMPTY;
			}

		}
	}

	public UICategory getCategory() {
		if (this.category == null) {
			this.category = new UICategory();
		}

		return category;
	}

	private void setCategory(UICategory category) {

		if (category.getCode().trim().equals("")) {
			this.setTextColor(this.context.getResources().getColor(
					R.color.lightRed));
			this.setText(context.getString(R.string.clickToEnter));

		} else {
			this.category.setCode(category.getCode());
			this.category.setName(category.getName());
			this.setTextColor(this.context.getResources().getColor(
					R.color.lightBlue));
			this.setText(category.getName());

		}

		// this.invalidate();

	}

	public void setCategory(String name, String code) {

		if (name == null || code == null) {
			this.category.setCode(EMPTY);
			this.category.setName(context.getString(R.string.noSetting));

		} else

		if (code.trim().equals("")) {
			this.category.setCode(EMPTY);
			this.category.setName(context.getString(R.string.clickToEnter));

		} else {
			this.category.setCode(code);
			this.category.setName(name);

		}

		refresh();

		// this.invalidate();
	}

	private void refresh() {

		if (isBind) {
			final SpannableString content = new SpannableString(
					this.category.getName());
			content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

			if (StringUtils.isBlank(this.category.getCode())) {
				this.setTextColor(this.context.getResources().getColor(
						R.color.lightRed));
			} else {
				this.setTextColor(this.context.getResources().getColor(
						R.color.black));
			}

			this.setText(content);
		} else {
			this.setTextColor(Color.BLACK);
			this.setText(this.category.getName());
		}

	}

	public List<UICategory> getCategorys() {
		return categorys;
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		String yyymmddd = String.format(FROMAT, year, monthOfYear + 1,
				dayOfMonth);
		// date.setText(yyymmddd);
		this.setCategory(yyymmddd, yyymmddd);

	}

	public String getTitle() {
		return title;
	}

}
